Repository navigation
Speed up documents with many indented code blocks - #775
Open
deadstrobe5 wants to merge 2 commits into
Open
deadstrobe5 wants to merge 2 commits into
deadstrobe5 wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
_do_code_blocks()regex ends with a negative lookahead,(?!([^<]|<(/?)span)*\</code\>), that keeps it from matching inside an existing<code>element (for syntax highlighting). After every indented code block, that lookahead scans forward to the next<that isn't a<span>. Plain Markdown has no such tag before the end of the document, so each block scans the rest of the text, and a document with many indented code blocks takes quadratic time.The lookahead can only match if the text contains
</code>, so this change adds it to the pattern only in that case. Without</code>in the text, the lookahead could never match, so dropping it gives the same result.many_code_blocks, the case this PR adds totest_redos.py(3000 blocks)</code>)Median of 5 alternating rounds in one process (3 for the 3000-block case), with the old module loaded next to the new one, CPython 3.14, Linux, on a busy machine.
python test.py -- -knownfailurepasses, and the output matches master on 86,000 random inputs (lists, indentation, code spans, inline HTML, links, backslash escapes,safe_modeand common extras). Documents that contain</code>keep the lookahead, so their timing does not change.